﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace TSMR01Xml
{
    public class TrafficEvent
    {
        public void SetData(byte[] data)
        {
            string xmlstr = Encoding.Default.GetString(data);
            //xmlstr = DeleteControlChar(xmlstr);
            XDocument doo = XDocument.Parse(xmlstr);
            var s = doo.Root;
            foreach (var item in s.Elements()) 
            {               
                switch (item.Name.ToString())
                {
                    // 节点
                    case "CamID":
                        CamID = item.Value.ToString();
                        break;
                    case "RecordID":
                        RecordID = item.Value.ToString();
                        break;
                    case "TollgateID":
                        TollgateID = item.Value.ToString();
                        break;
                    case "TollgateName":
                        TollgateName = item.Value.ToString();
                        break;
                    case "PassTime":
                        try
                        {
                            PassTime = DateTime.ParseExact(item.Value.ToString().Trim(), "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.CurrentCulture);
                        }
                        catch
                        {
                            PassTime = DateTime.ParseExact("20000101000000", "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
                        }
                        break;
                    case "PlaceCode":
                        PlaceCode = item.Value.ToString();
                        break;
                    case "PlaceName":
                        PlaceName = item.Value.ToString();
                        break;
                    case "LaneID":
                        LaneID = item.Value.ToString();
                        break;
                    case "LaneType":
                        LaneType = item.Value.ToString();
                        break;
                    case "LaneDirection":
                        LaneDirection = item.Value.ToString();
                        break;
                    case "LaneDescription":
                        LaneDescription = item.Value.ToString();
                        break;
                    case "LaneStatus":
                        LaneStatus = item.Value.ToString();
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 过滤不可见字符
        /// (十六进制值0x01)是无效的字符 问题 https://www.cnblogs.com/willingtolove/p/13121612.html
        /// </summary>
        /// <param name="sourceString">原始字符</param>
        /// <returns>删除后结果</returns>

        public string DeleteControlChar(string sourceString)
        {
            System.Text.StringBuilder sBuilder = new System.Text.StringBuilder();
            for (int i = 0; i < sourceString.Length; i++)
            {
                int Unicode = sourceString[i];
                if (Unicode > 31 && Unicode != 127)
                {
                    sBuilder.Append(sourceString[i].ToString());
                }
            }
            return sBuilder.ToString();
        }

        /// <summary>
        /// 卡口相机编号
        /// </summary>
        public string CamID { get; set; }
        /// <summary>
        /// 记录ID号
        /// </summary>
        public string RecordID { get; set; }
        /// <summary>
        /// 卡口编号
        /// </summary>
        public string TollgateID { get; set; }
        /// <summary>
        /// 卡口名称
        /// </summary>
        public string TollgateName { get; set; }
        /// <summary>
        /// 经过时刻
        /// </summary>
        public DateTime PassTime { get; set; }
        /// <summary>
        /// 地点编号
        /// </summary>
        public string PlaceCode { get; set; }
        /// <summary>
        /// 地点名称
        /// </summary>
        public string PlaceName { get; set; }
        /// <summary>
        /// 车道编号
        /// </summary>
        public string LaneID { get; set; }
        /// <summary>
        /// 车道类型
        /// </summary>
        public string LaneType { get; set; }
        /// <summary>
        /// 车道行驶方向
        /// </summary>
        public string LaneDirection { get; set; }
        /// <summary>
        /// 车道描述
        /// </summary>
        public string LaneDescription { get; set; }
        /// <summary>
        /// 车道状态
        /// </summary>
        public string LaneStatus { get; set; }
    }
}
